package com.caac.core.web.action.setting;

import com.caac.core.web.action.BaseAction;
import org.apache.struts.action.ActionMapping;
import com.caac.core.web.form.BaseForm;
import org.apache.struts.action.ActionForward;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.caac.core.dm.User;
import com.caac.core.util.HttpUtils;
import java.util.List;
import com.caac.core.service.ViewPropManager;
import com.caac.core.service.BaseManager;
import com.caac.core.config.ViewClass;
import com.caac.core.config.Config;
import java.util.ArrayList;
import com.caac.core.dm.ViewProp;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright Intolong 2004</p>
 * <p>Company: Intolong</p>
 * @author <a href="mailto:intolong@163.com">Intolong</a>
 * @version 1.0
 */

public class SaveStatPropAction extends BaseAction {

    protected ActionForward doExecute(ActionMapping mapping, BaseForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response) throws
            Exception {
        User user = this.getUserFromCache(request, response);
        String userName = null;
        BaseManager baseManager = (BaseManager)this.getBaseManager();
        if (user != null) {
            userName = user.getUsername();
            request.setAttribute("userName", userName);
        }
        List allProps = getView("statEmployee").getChildren();
        List selProps = baseManager.getObjects(ViewProp.class,"group_Name='statEmployee' and username='" +
                                               userName + "' order by priority");
        List unselProps = new ArrayList();
        if (selProps == null || selProps.size() == 0) {
            unselProps = getView("statEmployee").getDefDmChildren();
        } else {
            for (int i = 0; i < allProps.size(); i++) {
                ViewProp sp = (ViewProp) allProps.get(i);
                if (!selProps.contains(sp)) {
                    unselProps.add(sp);
                }
            }
        }
        request.setAttribute("selProps", selProps);
        request.setAttribute("unselProps", unselProps);
        return forward(mapping);
    }

    private static ViewClass getView(String groupName) {
        return Config.getInstance().getView(groupName);
    }

}
